import React from 'react'
import type { MenuProps } from 'antd';
import { Dropdown, Space, Avatar } from 'antd';
import { useNavigate } from 'react-router-dom';
import { useAppDispatch } from '@/store/hooks'
import { updateuserinfo } from '@/store/modules/userReducer';
import store from 'store'
const items: MenuProps['items'] = [
    {
        label: '设置',
        key: '/setting/systemsetting',
    },
    {
        label: '退出登录',
        key: '/loginout',
    },
];

type Props = {}

export default function Appdropdown({ }: Props) {
    const navigate = useNavigate();
    const dispatch = useAppDispatch();
    const onClick: MenuProps['onClick'] = ({ key }) => {
        if (key == '/loginout') {
            // 退出登录
            // 01: 清除本地的登录时存的信息
            dispatch(updateuserinfo({
                adminname: "",
                checkedkeys: [],
                role: 0,
                token: ""
            }));

            // 02: 清除本地单独登录时存的token
            store.remove('token')

        } else {
            navigate(key)
        }
    };
    return (
        <Dropdown menu={{ items, onClick }} trigger={['click']}>
            <a onClick={(e) => e.preventDefault()}>
                <Space>
                    <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
                </Space>
            </a>
        </Dropdown>
    )
}